Module# 06: Stack Lecture#23: Stack Using JCF
// Example 23.1:
Creating a stack
// This program
illustrates how to declare a stack as a collection
import java.util.Stack;
public class StackCreateDemo
{
public static void main(String a[]){
// Creating a stack of integers
Stack<Integer> stack = new Stack<>();
System.out.println(“Stack contains :
" + stack); // Printing the stack
System.out.println(“Is stack empty? : " + stack.empty());
}
}
// Example 23.2: A
stack with basic operations
// This program
illustrates some basic operations on a stack
import java.util.Stack;
public class StackOperationsDemo
{
public static void main(String a[]){
// Creating a stack
of integers
Stack<Integer> stack = new Stack<>();
System.out.println("Empty stack :
" + stack);
System.out.println("Empty stack :
" + stack.empty());
/* Following
statement will throw an exception if you pop an item from an empty stack. */
System.out.println("Empty stack :
Pop Operation : " + stack.pop());
// Inserting some data into the stack created
stack.push(1);
stack.push(22);
stack.push(333);
stack.push(4444);
// Print the entire stack now
System.out.println(“Data in the stack : "
+ stack);
System.out.println("Pop operation :
" + stack.pop());
System.out.println("After pop Operation :
" + stack);
System.out.println("The element at the top
: " + stack.peep());
System.out.println("After peep operation
: " + stack);
System.out.println("Search operation: " + stack.search(22));
System.out.println("Is stack empty? " + stack.empty());
}
}
// Example 23.3:
Loading a stack with an array of objects
/*This program
illustrates how a Stack object can be created with an array of characters.*/
import java.util.Stack;
public class ArrayToStackExample1
{
public static void main(String a[]){
// The input array of expression a+b*c-5
String[] expArray = { “a”, “+”, “b” , “*”, “c”, “-“, “5”};
// Create a Stack object of type String
Stack<String> stack = new Stack<String>();
// Loading the array into stack
for(String s : expArray){
stack.push(s);
}
System.out.println("The stack : " + stack);
}
}
// Example 23.4:
Another example - Array to stack loading
//To create a Stack
object with an array of integers.
import java.util.Stack;
public class ArrayToStackExample2
{
public static void main(String a[]){
Integer[] intArr = { 1001,1002,1003,1004};
Stack<Integer> stack = new Stack<>();
for(Integer i : intArr){
stack.push(i);
}
System.out.println(“The stack: " + stack);
stack.push(1001);
stack.search(1001);
System.out.println(“Verify the result: " + stack);
}
}
// Example 23.5: ArrayList to stack loading
/*To initialize a
stack with a given List of integers stored in a collection object of type ArrayList.*/
import java.util.*;
public class ArrayListToStackExample
{
public static void main(String a[]){
// Create a collection object of type ArrayList
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Stack<Integer> stack = new Stack<Integer>(); // Declare a Stack
object
stack.addAll(list); // Loading the stack
with the collection items
// Printing the first item in the stack
System.out.println("Top item in the stack
: " + stack.peep());
}
}
// Example 23.6:
Copying a stack to a collection object
/*The following
program illustrates the copy of a stack to a collection object of type ArrayList.*/
import java.util.*;
public class StackToArrayListCopyDemo
{
public static void main(String a[]){
Stack<Integer> stack = new Stack<Integer>();// The Input stack
stack.push(1);
stack.push(2);
stack.push(3);
// Create a
collection object of type ArrayList
ArrayList<Integer> list = new ArrayList<Integer>();
list.addAll(stack);// Copy the elements
in stack to the collection
System.out.println("Non-Empty stack :
" + stack);// Printing the Stack
System.out.println("Non-Empty List :
" + list);// Printing the ArrayList
}
}
// Example 23.7:
Basic operations like push, pop and peek
/*To create a stack
and Performing basic operations like push, pop and peek. */
import java.util.*;
public class StackExample
{
public static void main(String[] args) {
// Creating a Stack
Stack<String>stackOfCards = new Stack<>();
// Pushing new items
to the Stack
stackOfCards.push("Jack");
stackOfCards.push("Queen");
stackOfCards.push("King");
stackOfCards.push("Ace");
System.out.println("Stack =>
" + stackOfCards);
System.out.println();
// Popping items from the Stack
String
cardAtTop = stackOfCards.pop();
// Throws EmptyStackException if the stack is empty
System.out.println("Stack.pop() => " + cardAtTop);
System.out.println("Current Stack => " + stackOfCards);
System.out.println();
// Get the item at
the top of the stack without removing it
cardAtTop = stackOfCards.peek();
System.out.println("Stack.peek()
=> " + cardAtTop);
System.out.println("Current Stack => " + stackOfCards);
}
}
// Example 23.8:
Checking empty, size and searching on stack
/*To Check: If the
stack is empty | Find the size of the stack. | Search for an element in the Stack.*/
import java.util.Stack;
public class StackSizeSearchExample
{
public static void main(String[] args) {
Stack<String>stackOfCards = new Stack<>();
stackOfCards.push("Jack");
stackOfCards.push("Queen");
stackOfCards.push("King");
stackOfCards.push("Ace");
System.out.println("Stack : " + stackOfCards);
// Check if the Stack
is empty
System.out.println("Is Stack empty? :
" + stackOfCards.isEmpty());
// Find the size of Stack
System.out.println("Size of Stack :
" + stackOfCards.size());
// Search for an
element
// The search() method returns the
// 1-based position
of the element from the top of the stack
// It returns -1 if
the element was not found in the stack
int position = stackOfCards.search("Queen");
if(position != -1) {
System.out.println("Found element
\"Queen\" at position : " + position);
} else {
System.out.println("Element not
found");
}
}
}
// Example 23.9:
Iterating a stack
/* Iterate using: forEach()
| iterator() | forEachRemaining() | listIterator() */
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class IterateOverStackExample
{
public static void main(String[] args) {
Stack<String>stackOfPlates = new Stack<>();
stackOfPlates.add("Plate 1");
stackOfPlates.add("Plate 2");
stackOfPlates.add("Plate 3");
stackOfPlates.add("Plate 4");
System.out.println("=== Iterate over Stack using forEach()
method ===");
stackOfPlates.forEach(plate -> {
System.out.println(plate);
});
System.out.println("\n=== Iterate
over a Stack using iterator() ===");
Iterator<String>platesIterator = stackOfPlates.iterator();
while (platesIterator.hasNext()) {
String plate = platesIterator.next();
System.out.println(plate);
}
System.out.println("\n=== Iterate
over a Stack using iterator() method ===");
itr = stackOfPlates.iterator();
while (itr.hasNext()) {
String plate = itr.next();
System.out.println(plate);
}
System.out.println("\n=== Iterate
over a Stack from TOP to BOTTOM using listIterator()
===");
// ListIterator
allows you to traverse in both forward and backward directions.
// We'll start from
the top of the stack and traverse backwards.
ListIterator<String>platesListIterator = stackOfPlates.listIterator(stackOfPlates.size());
while (platesListIterator.hasPrevious()) {
String plate = platesListIterator.previous();
System.out.println(plate);
}
}
}